﻿using System;
using System.Data;
using System.Globalization;
using System.Threading;
using DayPilot.Web.Ui;
using Model;
using Service;


public partial class laborResourceView : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");

        if (!IsPostBack)
        {
            int employeeId;
            if (Request.QueryString["assignEmployeeId"] != null)
                employeeId = new Utility().ConvertToInteger(Request.QueryString["assignEmployeeId"]);
            else employeeId = new Utility().ConvertToInteger(Session["assignEmployeeId"]);

            Employee employee = new StockService().GetEmployee(employeeId);
            if (employee == null) return;
            lblTitle.Text = employee.EmployeeName + " on " + Day.ToString(Resources.Resource.DateEnglishFormat) +" (mm/dd/yyyy)";
            Calendar1.SelectedDate = Day;
            Calendar1.VisibleDate = Day;
            DayPilotCalendar1.StartDate = Day;
            DayPilotCalendar1.EndDate = Day;
            DataBind();
            
        }
    }

    #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
       
        InitializeComponent();
        base.OnInit(e);
    }

   
    private void InitializeComponent()
    {
        Calendar1.DayRender += new System.Web.UI.WebControls.DayRenderEventHandler(Calendar1_DayRender);
        DayPilotCalendar1.FreeTimeClick += new DayPilot.Web.Ui.FreeClickDelegate(DayPilotCalendar1_FreeTimeClick);
       

    }
    #endregion

    private void Calendar1_DayRender(object sender, System.Web.UI.WebControls.DayRenderEventArgs e)
    {
        string fontWeight = "normal";
        if (isThereEvent(e.Day.Date)) fontWeight = "bold";
            string color = "black";
        if (e.Day.IsOtherMonth)
            color = Calendar1.OtherMonthDayStyle.ForeColor.Name;

        e.Cell.Text = String.Format("<a href='laborResourceView.aspx?day={0:d}&employeeId='"+Session["assignEmployeeId"]+ "style='color: " + color + ";text-decoration:none; font-weight:" + fontWeight + "'>{1}</a>", e.Day.Date, e.Day.Date.Day);
    }

    protected DateTime Day
    {
        get
        {
            if (Request.Params["day"] != null)
            {
                try
                {
                    return Convert.ToDateTime(Request.Params["day"]);
                }
                catch
                {
                    // do nothing, continue to return today
                }
            }
            return DateTime.Now;
        }
    }


    protected DataTable getData
    {
        get
        {


            DataTable tableGet = new DataTable();
            WODirectService  woDirectService = new WODirectService();
            tableGet = woDirectService.GetAvailability(GetWhereClause());
            DataTable dt = new DataTable();
            dt.Columns.Add("id", typeof(string));
            dt.Columns.Add("start", typeof(DateTime));
            dt.Columns.Add("end", typeof(DateTime));
            dt.Columns.Add("name", typeof(string));

            foreach (DataRow row in tableGet.Rows)
            {

                DataRow dr = dt.NewRow();
                DateTime startTime = DateTime.Now;
                DateTime endTime = DateTime.Now;

                if (row["plantime"] != DBNull.Value)
                {
                    startTime = DateTime.Parse(row["plantime"].ToString());
                    if (row["estimateTimeInMinute"] != DBNull.Value && row["plantime"] != DBNull.Value)
                        endTime = DateTime.Parse(row["plantime"].ToString()).AddMinutes(double.Parse(row["estimateTimeInMinute"].ToString()));
                    else endTime = startTime;
                }

                dr["id"] = row["planningId"];
                dr["start"] = startTime;
                dr["end"] = endTime;
                dr["name"] = "Work Order Nº:" + row["workOrderId"] + ", Criteria : " + row["criteria"] + ", Problem : "+ row["problem"];
                dt.Rows.Add(dr);

            }
            return dt;
          }

    }
    private string GetWhereClause()
    {
        string str = string.Empty;
        int employeeId = 0;
        DateTime dateSearch = DateTime.Now;
        if (Request.QueryString["day"] != null)
            dateSearch = DateTime.Parse(Request.QueryString["day"]);

        if (Session["assignEmployeeId"] != null)
            employeeId = new Utility().ConvertToInteger(Session["assignEmployeeId"]);

        str = " WHERE ( Day(PLANDATE) = " + dateSearch.Day + " AND Month(PLANDATE)=" + dateSearch.Month + " AND Year(PLANDATE)= " + dateSearch.Year + ")";
        str  +=" AND  ( Day(PLANTIME) = "+ dateSearch.Day + " AND Month(PLANTIME)="+ dateSearch.Month+ " AND Year(PLANTIME)= "+ dateSearch.Year +")";
        str += " AND planning.EmployeeId  =  " + employeeId;
        return str;

    }


  
    private bool isThereEvent(DateTime date)
    {
        DateTime today = DateTime.Now;
        DateTime tomorrow = today.AddDays(1);
        DateTime anotherDay = today.AddDays(3);

        // there are events today
        if ((date.DayOfYear == today.DayOfYear) && (date.Year == today.Year))
            return true;

        // there are events tomorrow
        if ((date.DayOfYear == tomorrow.DayOfYear) && (date.Year == tomorrow.Year))
            return true;

        // there are events on another day
        if ((date.DayOfYear == anotherDay.DayOfYear) && (date.Year == anotherDay.Year))
            return true;

        return false;
    }

    private void DayPilotCalendar1_FreeTimeClick(DateTime start)
    {
        
    }

    protected void LinkButtonSwitch1224_Click(object sender, System.EventArgs e)
    {
        if (DayPilotCalendar1.TimeFormat == TimeFormat.Clock12Hours)
            DayPilotCalendar1.TimeFormat = TimeFormat.Clock24Hours;
        else
            DayPilotCalendar1.TimeFormat = TimeFormat.Clock12Hours;
    }

    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        if (DayPilotCalendar1.NonBusinessHours == NonBusinessHoursBehavior.AlwaysVisible)
            DayPilotCalendar1.NonBusinessHours = NonBusinessHoursBehavior.HideIfPossible;
        else
            DayPilotCalendar1.NonBusinessHours = NonBusinessHoursBehavior.AlwaysVisible;
    }

    protected void LinkButton2_Click(object sender, EventArgs e)
    {
        if (DayPilotCalendar1.HourHeight == 40)
            DayPilotCalendar1.HourHeight = 30;
        else
            DayPilotCalendar1.HourHeight = 40;
    }

    protected void LinkButton3_Click(object sender, EventArgs e)
    {
        if (DayPilotCalendar1.FreetimeClickHandling == UserActionHandling.JavaScript)
        {
            DayPilotCalendar1.FreetimeClickHandling = UserActionHandling.PostBack;
            DayPilotCalendar1.EventClickHandling = UserActionHandling.PostBack;
        }
        else
        {
            DayPilotCalendar1.FreetimeClickHandling = UserActionHandling.JavaScript;
            DayPilotCalendar1.EventClickHandling = UserActionHandling.JavaScript;
        }

    }


    protected void DayPilotCalendar1_FreeTimeClick1(DateTime start)
    {
        Session["planTime"] = start;
        Label1.Text = start.ToString();
        Response.Redirect(Session["oringalPage"].ToString());
    }
    protected void DayPilotCalendar1_EventClick(string pk)
    {
        Label1.Text = pk;
    }
}
